# encoding:utf-8
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line, Pie, Tab
from pyecharts.components import Table
from pyecharts.faker import Faker
import pandas as pd
from pyecharts.options import ComponentTitleOpts

tab = Tab()


class PyechartUtils:
    def add_lines(self, df: pd.DataFrame, title=''):
        lines = Line()
        lines.set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=False),
            xaxis_opts=opts.AxisOpts(type_="category"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
        ).add_xaxis(xaxis_data=list(df.index))

        for col in df.columns:
            lines.add_yaxis(
                series_name=col,
                y_axis=df[col],
                symbol=col,
                is_symbol_show=True,
                label_opts=opts.LabelOpts(is_show=False),
            )
        tab.add(lines, title)

    def add_table(self, df: pd.DataFrame, title=''):

        headers = list(df.columns)
        items = df.T.to_dict(orient='list')
        rows = []
        for row in items.values():
            rows.append(row)

        tb = (
            Table()
                .add(headers, rows)
                .set_global_opts(
                title_opts=ComponentTitleOpts(title=title),
            )
        )
        tab.add(tb, title)

    def render(self, filename=''):
        tab.render("htmls/{}".format(filename))
